package com.aliwo.controller;

import com.aliwo.domain.Product;
import com.aliwo.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author xuyayuan
 * @data 2021年07月09日
 */
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/getById/{id}")
    public Object getProductById(@PathVariable Integer id) {
        // 设置休眠时间
        //try {
        //    TimeUnit.SECONDS.sleep(2);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        Product product = productService.findByid(id);
        Product result = new Product();
        BeanUtils.copyProperties(product, result);
        result.setProductName(product.getProductName() + " port: " + port);
        return result;
    }
    @GetMapping("/findById")
    public Object findProductById(@RequestParam("id") Integer id) {
        Product product = productService.findByid(id);
        Product result = new Product();
        BeanUtils.copyProperties(product, result);
        result.setProductName(product.getProductName() + " port: " + port);
        return result;
    }

    @GetMapping("/detail")
    public List<Product> getProductDetails() {
        List<Product> productList = productService.getProductDetails();
        return productList;
    }
}
